package servlet;

import anno.Autowired;
import anno.RequestBody;
import anno.ResponseBody;
import dao.HallDAO;
import pojo.Hall;
import utils.ApiResult;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet("/hall")
public class HallServlet extends BaseServlet {

    @Autowired
    private HallDAO hallDAO;

    /**
     * 添加影厅
     *
     * @param req  请求对象
     * @param resp 响应对象
     * @param hall 影厅对象
     * @return ApiResult 响应结果
     * @throws SQLException SQL异常
     */
    @ResponseBody
    public ApiResult addHall(HttpServletRequest req, HttpServletResponse resp, @RequestBody Hall hall) throws SQLException {
        hallDAO.insert(hall);
        return new ApiResult(200, "影厅添加成功", null);
    }

    /**
     * 更新影厅
     *
     * @param req  请求对象
     * @param resp 响应对象
     * @param hall 影厅对象
     * @return ApiResult 响应结果
     * @throws SQLException SQL异常
     */
    @ResponseBody
    public ApiResult updateHall(HttpServletRequest req, HttpServletResponse resp, @RequestBody Hall hall) throws SQLException {
        hallDAO.update(hall);
        return new ApiResult(200, "影厅更新成功", null);
    }

    /**
     * 删除影厅
     *
     * @param req  请求对象
     * @param resp 响应对象
     * @param id   影厅ID
     * @return ApiResult 响应结果
     * @throws SQLException SQL异常
     */
    @ResponseBody
    public ApiResult deleteHall(HttpServletRequest req, HttpServletResponse resp, String id) throws SQLException {
        hallDAO.deleteById(Integer.parseInt(id));
        return new ApiResult(200, "影厅删除成功", null);
    }

    /**
     * 根据ID查询影厅
     *
     * @param req  请求对象
     * @param resp 响应对象
     * @param id   影厅ID
     * @return ApiResult 响应结果
     * @throws SQLException SQL异常
     */
    @ResponseBody
    public ApiResult getHallById(HttpServletRequest req, HttpServletResponse resp, String id) throws SQLException {
        Hall hall = hallDAO.findById(Integer.parseInt(id));
        return new ApiResult(200, "影厅查询成功", hall);
    }

    /**
     * 分页模糊查询影厅
     *
     * @param req      请求对象
     * @param resp     响应对象
     * @param keyword  模糊查询关键字
     * @param pageNum  当前页码
     * @param pageSize 每页记录数
     * @return ApiResult 响应结果
     * @throws SQLException SQL异常
     */
    @ResponseBody
    public ApiResult findHallsByPageAndName(HttpServletRequest req, HttpServletResponse resp, String keyword, String pageNum, String pageSize) throws SQLException {
        int page = Integer.parseInt(pageNum);
        int size = Integer.parseInt(pageSize);
        Map<String, Object> result = hallDAO.findHallsByPageAndName(keyword, page, size);
        return new ApiResult(200, "分页模糊查询影厅成功", result);
    }
    /**
     * 获取所有影厅
     *
     * @param req  请求对象
     * @param resp 响应对象
     * @return ApiResult 响应结果
     * @throws SQLException SQL异常
     */
    @ResponseBody
    public ApiResult getAllHalls(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        List<Hall> halls = hallDAO.findAll();
        return new ApiResult(200, "获取所有影厅成功", halls);
    }
}